//
//  UINavigationBar+Awesome.m
//  LTNavigationBar
//
//  Created by ltebean on 15-2-15.
//  Copyright (c) 2015 ltebean. All rights reserved.
//

#import "UINavigationBar+Awesome.h"
#import <objc/runtime.h>


#define NavBackButtonTag 20016

#define navBottomGreenImgTag 20017

#define NavEditButtonTag 20018


#define kNaviTitleLabelMargin       40
#define kNaviTitleLabelHeight       44

#define kNaviEditButtonMarginLeft    5
#define kNaviEditButtonMarginTop    5
#define kNaviEditButtonMarginRight  8

#define kNaviEditButtonWidth        60
#define kNaviEditButtonHeight       44

@implementation UINavigationBar (Awesome)
static char overlayKey;

- (UIView *)overlay
{
    return objc_getAssociatedObject(self, &overlayKey);
}

- (void)setOverlay:(UIView *)overlay
{
    objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
    if (!self.overlay) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 20)];
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
    }
     self.overlay.userInteractionEnabled = YES;
    self.overlay.backgroundColor = backgroundColor;
}

- (void)lt_setTranslationY:(CGFloat)translationY
{
    self.transform = CGAffineTransformMakeTranslation(0, translationY);
}

- (void)lt_setElementsAlpha:(CGFloat)alpha
{
    [[self valueForKey:@"_leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    [[self valueForKey:@"_rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    UIView *titleView = [self valueForKey:@"_titleView"];
    titleView.alpha = alpha;
//    when viewController first load, the titleView maybe nil
    [[self subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
            obj.alpha = alpha;
            *stop = YES;
        }
    }];
}

- (void)lt_reset
{
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
    self.overlay = nil;
}

- (void)setTitle:(NSString *)title
{
    UILabel* _titleLabel = nil;
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kNaviTitleLabelMargin, kNaviOrigenY, kScreenWidth - kNaviTitleLabelMargin*2, kNaviTitleLabelHeight)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = RGB(255, 255, 255);
        _titleLabel.userInteractionEnabled = YES;
        
        //        if (didTap)
        //        {
        //            UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:didTap];
        //            [titleLabel addGestureRecognizer:tapGesture];
        //        }
        
        [self.overlay addSubview:_titleLabel];
    }
    _titleLabel.text = title;
    
   // [self.overlay bringSubviewToFront:_backButton];
    
   // [self adjustTitleWidth];
}

//- (void)adjustTitleWidth
//{
//    if (_titleLabel)
//    {
//        int margin = _backButton?kNaviTitleLabelMargin:66;
//        if (self.editBtn && self.editBtn.hidden == NO) {
//            margin = kScreenWidth - self.editBtn.frame.size.width + 5;
//        }
//        CGRect labelFrame = CGRectMake(margin, kNaviOrigenY, kScreenWidth - margin*2, kNaviTitleLabelHeight);
//        [_titleLabel setFrame:labelFrame];
//    }
//}

- (void)customNaviLeftButton:(id)target action:(SEL) action title:(NSString *)title
{
//    if (_backButton)
//    {
//        [_backButton removeFromSuperview];
//        _backButton = nil;
//    }
   UIButton* _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setTitle:title forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
    _backButton.titleLabel.font = [UIFont systemFontOfSize:16];
    //  [_backButton setBackgroundColor:[UIColor redColor]];
    // _backButton.frame = CGRectMake(10, kNaviOrigenY, 30, 44);
    _backButton.frame = CGRectMake(0, kNaviOrigenY, kNaviEditButtonWidth, kNaviEditButtonHeight);
    [_backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    _backButton.tag = 1001;
    [self.overlay addSubview:_backButton];
}

- (void)customNaviRightButton:(id)target action:(SEL) action title:(NSString *)title
{
//    if (_editBtn)
//    {
//        [_editBtn removeFromSuperview];
//        _editBtn = nil;
//    }
    UIButton* _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editBtn setTitle:title forState:UIControlStateNormal];
    [_editBtn setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
    _editBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _editBtn.frame = CGRectMake(-kNaviEditButtonMarginLeft, kNaviOrigenY, kNaviEditButtonWidth, kNaviEditButtonHeight);
    _editBtn.left = kScreenWidth - kNaviEditButtonWidth;
    [_editBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self.overlay addSubview:_editBtn];
}
@end
